import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.*;

/**
 * This class is for the patient's bill.
 * @author Matt Lee
 *
 */

//NOTE: This class MUST be displayed within a panel or subpanel that shows
//what is the patient's invoice.

//Update 4/14/11: The plan is to call a specific treatment record to determine a 
//patient's invoice.
public class PatientInvoice{
	private Patient patient;

	private String visitDate;

	private int month;
	
	private int year;
	
	private Doctor attendingDoctor;
	
	private double patientBill;
	
	private static Logger patientInvoiceLog = 
		Logger.getLogger(PatientInvoice.class.getName());

	private TreatmentRecord record; //Added new parameter.
	
	static{
		ConsoleHandler console = new ConsoleHandler();
		console.setLevel(Level.FINEST);
		console.setFormatter(new ThunderDragonHtmlFormatter());
		patientInvoiceLog.addHandler(console);
		patientInvoiceLog.setLevel(Level.ALL);
		
		try {
			FileHandler fh = new FileHandler("PatientInvoiceLog.txt");
			fh.setFormatter(new ThunderDragonHtmlFormatter());
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			patientInvoiceLog.severe("Failure: Security exception thrown" +
					" when trying to create a new handler " +
					"in PatientInvoice.java.");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			patientInvoiceLog.severe("Failure: I/O exception thrown" +
					" when trying to create a new handler" +
					" in PatientInvoice.java.");
			e.printStackTrace();
		}
		
	}

	
	public PatientInvoice(TreatmentRecord treatRecord){
		record = treatRecord;
		patient = record.getPatient();
		visitDate = record.getDate();
		attendingDoctor = record.getDoctor();
		patientBill = 0;
		month = record.getMonth();
		
	}

	/**
	 * This method calculates the TOTAL bill for the patient.
	 * @return the double that represents the total bill calculated.
	 */
	private double CalculateBill(){
		
		return this.PrescriptionsBill() + this.LabworkBill();
	}
	
	/**
	 * This calculates the total costs of prescriptions. 
	 *  This method was created to be used
	 * for the patient invoice so that
	 * @return The total cost of just prescriptions.
	 */
	private double PrescriptionsBill(){
		double prescripCost = 0;
		TreatmentRecord currentRecord = this.getRecord();
		DoctorOrders docItems = currentRecord.getDocOrders();
		ArrayList<Prescription> prescriptions = docItems.getPrescription();
		for(int k=0; k < prescriptions.size(); k++){
			Prescription prescript = prescriptions.get(k);
			prescripCost = prescripCost+prescript.getPrice();
			//patientBill = patientBill + prescript.getPrice();  //This is a STUB.  This will be fixed later.
		}
		
		return prescripCost;
		
	}
	
	private double LabworkBill(){
		double labWorkCost = 0;
		TreatmentRecord currentRecord = this.getRecord();
		DoctorOrders docItems = currentRecord.getDocOrders();
		ArrayList<Labwork> Labworks = docItems.getLabWork();
		for(int k=0; k < Labworks.size(); k++){
			Labwork lab = Labworks.get(k);
			labWorkCost = labWorkCost+lab.getPrice();
			//patientBill = patientBill + prescript.getPrice();  //This is a STUB.  This will be fixed later.
		}
		
		return labWorkCost;
		
	}
	
	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}
	
	public String getVisitDate() {
		return visitDate;
	}
	
	public void setVisitDate(String visitDate) {
		this.visitDate = visitDate;
	}
	
	public Doctor getAttendingDoctor() {
		return attendingDoctor;
	}
	
	public void setAttendingDoctor(Doctor attendingDoctor) {
		this.attendingDoctor = attendingDoctor;
	}
	
	public double getPatientBill() {
		return this.CalculateBill();
	}
	
	public void setPatientBill(double patientBill) {
		this.patientBill = patientBill;
	}
	
	public static Logger getPatientInvoiceLog() {
		return patientInvoiceLog;
	}
	
	public static void setPatientInvoiceLog(Logger patientInvoiceLog) {
		PatientInvoice.patientInvoiceLog = patientInvoiceLog;
	}
	
	public TreatmentRecord getRecord() {
		return record;
	}
	
	public void setRecord(TreatmentRecord record) {
		this.record = record;
	}
	
	public String toString(){
		return "The Patient owes: " + this.getPatientBill();
	}
}